﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.DMS.Entities;
using System.Data;
using System.Xml;
using ESF.General;
using ESF.General.Entity;

#endregion


namespace ESF.DMS.DataAccess
{
	public static class AdoFileReferenceLevel
	{
		#region XML
		 
		public static string GetFilterXML (List<FileReferenceLevel> pFilters)
		{
			StringBuilder stbMultiLevelFilter = new StringBuilder();
			
			XmlWriter xmlWriter = XmlTextWriter.Create(stbMultiLevelFilter, new XmlWriterSettings() { OmitXmlDeclaration = true });


			AdoFileReferenceLevel.ConvertToXML(xmlWriter, pFilters);
			xmlWriter.Close();

			return stbMultiLevelFilter.ToString ();
		}


		public static void ConvertToXML<O>(XmlWriter pXmlWriter, List<O> pItems) where O : FileReferenceLevel
		{
			if (DataHelper.IsEmpty (pItems))
			{
				return;
			}

			pXmlWriter.WriteStartElement("FRLs"); 
			
			foreach (O item in pItems)
			{
				ConvertToXML<O> (pXmlWriter, item);
			}
			
			pXmlWriter.WriteEndElement();
		}


		public static void ConvertToXML<O>(XmlWriter pXmlWriter, O pItem, bool pAvoidMainElement = false) where O : FileReferenceLevel
		{
			if (!pAvoidMainElement)
			{
				pXmlWriter.WriteStartElement("FileReferenceLevel");
			}

			pXmlWriter.WriteAttributeString ("Name", pItem.Name);
			pXmlWriter.WriteAttributeString("Level", pItem.Level.ToString());

			if (pItem.FileReference != null && pItem.FileReference.ID != null)
			{
				pXmlWriter.WriteStartElement ("FileReference");
					pXmlWriter.WriteAttributeString ("ID", pItem.FileReference.ID.ToString ());
				pXmlWriter.WriteEndElement ();
			}
			
			
			if (!pAvoidMainElement)
			{
				pXmlWriter.WriteEndElement ();
			}
		}

		#endregion


		public static List<BasicReference> List (FileReferenceLevel pSample, List<FileReferenceLevel> pFilterOtherLevels)
		{
			IDbCommand cmd;
			
			
			cmd = DBContext.R.CreateSPCommand ("esf_files.P_ListFileReferenceLevel");
			
			if (pSample.FileReference != null)
			{
				DBContext.R.AddIDParameter (cmd, "pMonitoredDirectoryID", pSample.FileReference.MonitoredDirectory);
			}

			DBContext.R.AddParameter (cmd, "pLevel", pSample.Level);
			DBContext.R.AddParameter (cmd, "pName", pSample.Name, General.Text.ComparisonTypes.LikeFull);

			DBContext.R.AddParameter(cmd, "pXMLMultiLevelsSearch", AdoFileReferenceLevel.GetFilterXML(pFilterOtherLevels));

			return DBContext.R.GetListBasicReference (cmd);
		}


		#region Write

		public static void DeleteAll (FileReference pFile)
		{
			IDbCommand cmd;


			cmd = DBContext.W.CreateSPCommand ("esf_files.P_DeleteAllFileReferenceLevel");

			DBContext.W.AddIDParameter (cmd, "pFileReferenceID", pFile.ID);


			DBContext.W.Execute (cmd);
		}


		public static void Insert (FileReferenceLevel pItem)
		{
			IDbCommand cmd;


			cmd = DBContext.W.CreateSPCommand ("esf_files.P_InsertFileReferenceLevel");

			DBContext.W.AddIDParameter (cmd, "pFileReferenceID", pItem.FileReference.ID);
			DBContext.W.AddParameter (cmd, "pLevel", pItem.Level);
			DBContext.W.AddParameter (cmd, "pName", pItem.Name);

			DBContext.W.Execute(cmd);
		}

		#endregion
	}
}
